package com.woniuxy01;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author 作者
 * @create 2025-11-04 9:38
 */
public class Test02 {
    public static void main(String[] args) {
        Student s1=new Student(1,"zs",11);
        Student s2=new Student(2,"ls",15);
        Student s3=new Student(3,"ww",12);
        Student[] students=new Student[]{s1,s2,s3};
        System.out.println("排序前");
        System.out.println(Arrays.toString(students));
        //自然排序，要求Student类自己实现Comparable接口的compareTo()方法
        Arrays.sort(students);
        System.out.println("年龄升序:排序后");
        System.out.println(Arrays.toString(students));
        System.out.println("年龄降序:排序后");
        //自定义排序,可以扩展排序规则，实现Comparotor接口的comapre()方法
        Arrays.sort(students,new StudentAgeDescComparotor());
        System.out.println(Arrays.toString(students));
        System.out.println("姓名升序:排序后");
        //Arrays.sort(数组,接口实现类对象);
        Arrays.sort(students,new StudentNameAscComparotor());
        System.out.println(Arrays.toString(students));
        System.out.println("姓名降序:排序后");
        Arrays.sort(students,new StudentNameDescComparotor());
        System.out.println(Arrays.toString(students));
        System.out.println("学号降序:排序后");

        //sort(数组，匿名内部类)
        Arrays.sort(students,new Comparator(){
            @Override
            public int compare(Object o1, Object o2) {
                if(o1 instanceof Student && o2 instanceof Student){
                    Student one= (Student) o1;
                    Student other= (Student) o2;
                    return other.getSid()-one.getSid();
                }
                return -1;
            }
        });
        System.out.println(Arrays.toString(students));

    }
}
